package Graphics;

import HelperClasses.GlobalFunctions;
import HelperClasses.Vektor;
import Logic.Stone;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 *
 * @author johannes
 */
public class FieldGraphics {

    private boolean isHouse = false;
    private ArrayList<Stone> Stones;
    private Point wischer;
    private Point player;
    private Vektor shotVector;
    private Image field = null;
    private Image renderedField = null;
    private Image renderedHouse = null;
    public static final int internalWidth = 100;
    public static final int internalHeight = 1000;
    public static final int stoneSize = 16;
    public static final Point houseCenter = new Point(50, 120);
    public static int hogLinePos = 120;
    private final int playerHeight = 36;
    private final int playerWidth = 26;
    private final boolean debug = true;

    public FieldGraphics(boolean isHouse) {
        this.isHouse = isHouse;
    }

    public Vektor getShotVector() {
        return shotVector;
    }

    public void setShotVector(Vektor shotVector) {
        this.shotVector = shotVector;
    }

    public Point getPlayer() {
        return player;
    }

    public void setPlayer(Point player) {
        this.player = player;
    }

    public ArrayList<Stone> getStones() {
        return Stones;
    }

    public void setStones(ArrayList<Stone> Stones) {
        this.Stones = Stones;
    }

    public Point getWischer() {
        return wischer;
    }

    public void setWischer(Point wischer) {
        this.wischer = wischer;
    }

    public void drawHouse(Graphics g, int width, int height) {
        drawHouse(g, width, height, false);
    }

    public void drawHouse(Graphics g, int width, int height, boolean render) {
        if (render || (renderedHouse == null)) {
            renderImage();
        }
        
        // downscalen 
        Dimension dim = GlobalFunctions.getScaledDimension(internalWidth, (int) (internalWidth * 2.4), width, height);

        g.drawImage(renderedHouse.getScaledInstance((int) dim.getWidth(),
                (int) dim.getHeight(), Image.SCALE_SMOOTH), (int) ((width - dim.getWidth()) / 2), 0, null);
    }

    public void drawFull(Graphics g, int width, int height) {
        renderImage();

        // säubern
//        g.setColor(Color.white);
//        g.fillRect(0, 0, width, height);

        // downscalen 
        Dimension dim = GlobalFunctions.getScaledDimension(internalWidth, internalHeight, width, height);
        g.drawImage(renderedField.getScaledInstance((int) dim.getWidth(),
                (int) dim.getHeight(), Image.SCALE_SMOOTH), (int) ((width - dim.getWidth()) / 2), 0, null);
    }

    public void renderImage() {
        if (field == null) {
            field = new BufferedImage(internalWidth, internalHeight, BufferedImage.TYPE_INT_RGB);
            renderedField = new BufferedImage(internalWidth, internalHeight, BufferedImage.TYPE_INT_RGB);
            renderedHouse = new BufferedImage(internalWidth, (int) (internalWidth * 2.4), BufferedImage.TYPE_INT_RGB);
            Graphics h = field.getGraphics();
            ((Graphics2D) h).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

            // Untergrund
            h.setColor(Color.white);
            h.fillRect(0, 0, internalWidth, internalHeight);
            // Außenlinie
            h.setColor(Color.BLACK);
            h.drawRect(0, 0, internalWidth - 1, internalHeight - 1);

            // House
            h.setColor(Color.red);
            h.fillOval(10, 80, 80, 80);
            h.setColor(Color.white);
            h.fillOval(20, 90, 60, 60);
            h.setColor(Color.blue);
            h.fillOval(30, 100, 40, 40);
            h.setColor(Color.white);
            h.fillOval(40, 110, 20, 20);

            // Linien
            // Center Line
            h.setColor(Color.black);
            h.drawLine(internalWidth / 2, 0, internalWidth / 2, internalHeight);
            // Tee Line
            h.drawLine(0, houseCenter.y, internalWidth, houseCenter.y);
            // Back Line
            h.drawLine(0, houseCenter.y - 80, internalWidth, houseCenter.y - 80);
            // Hog Line
            h.drawLine(0, houseCenter.y + hogLinePos, internalWidth, houseCenter.y + hogLinePos);
            // Start Line
            h.drawLine(0, internalHeight - 160, internalWidth, internalHeight - 160);

        }


        Graphics graph = renderedField.getGraphics();
        ((Graphics2D) graph).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        // kopieren in neues Bild
        graph.setColor(Color.white);
        graph.fillRect(0, 0, internalWidth, internalHeight);
        graph.drawImage(field, 0, 0, null);
        //field.getScaledInstance(internalWidth, internalHeight, Image.SCALE_FAST);

        // malen
        // Steine:
        if (Stones != null) {
            Point p = null;
            for (Stone s : Stones) {
                p = s.getPos();
                // Rand
                graph.setColor(Color.darkGray);
                graph.fillOval((int) (p.getX() - (stoneSize / 2)), (int) (p.getY() - (stoneSize / 2)),
                        stoneSize, stoneSize);
                // Stein               
                graph.setColor(s.getColor());
                graph.fillOval((int) (p.getX() - ((stoneSize - 4) / 2)), (int) (p.getY() - ((stoneSize - 4) / 2)),
                        stoneSize - 4, stoneSize - 4);

                if (debug) {
                    //Impuls
                    graph.setColor(Color.BLACK);
                    graph.drawLine(p.x, p.y, (int) (p.getX() + s.getImpuls().getX()), (int) (p.getY() + s.getImpuls().getY()));
                    // Position aufmalen
                    Graphics h = field.getGraphics();
                    ((Graphics2D) h).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
                    h.setColor(Color.GRAY);
                    h.drawLine(s.getPos().x, s.getPos().y, s.getPos().x, s.getPos().y);
                }
            }
        }


        // Spieler:
        if (player != null) {
            graph.setColor(Color.green);
            graph.fillRect((int) (player.getX() - playerWidth), (int) player.getY(), playerWidth, playerHeight);
        }

        // Wischer:
        if (wischer != null) {
            graph.setColor(Color.orange);
            graph.fillRect((int) (wischer.getX() - playerWidth), (int) player.getY(), playerWidth, playerHeight);
        }

        // ShotVector
        if (shotVector != null) {
            graph.setColor(Color.BLUE);
            graph.drawLine((internalWidth / 2), internalHeight, (int) ((internalWidth / 2) + shotVector.getX()), (int) (internalHeight + shotVector.getY()));
            graph.drawLine((internalWidth / 2) + 1, internalHeight, (int) ((internalWidth / 2) + shotVector.getX() + 1), (int) (internalHeight + shotVector.getY()));
            graph.drawLine((internalWidth / 2) + 2, internalHeight, (int) ((internalWidth / 2) + shotVector.getX() + 2), (int) (internalHeight + shotVector.getY()));
            graph.drawLine((internalWidth / 2) + 3, internalHeight, (int) ((internalWidth / 2) + shotVector.getX() + 3), (int) (internalHeight + shotVector.getY()));
            graph.drawLine((internalWidth / 2) - 1, internalHeight, (int) ((internalWidth / 2) + shotVector.getX() - 1), (int) (internalHeight + shotVector.getY()));
            graph.drawLine((internalWidth / 2) - 2, internalHeight, (int) ((internalWidth / 2) + shotVector.getX() - 2), (int) (internalHeight + shotVector.getY()));
            graph.drawLine((internalWidth / 2) - 3, internalHeight, (int) ((internalWidth / 2) + shotVector.getX() - 3), (int) (internalHeight + shotVector.getY()));
        }

        // House separieren
        renderedHouse.getGraphics().drawImage(renderedField, 0, -20, null);
    }
}
